<%@ include file="/taglibs.jsp"%>
<jsp:directive.page import="java.util.Random"/>
<jsp:directive.page import="java.util.Date"/>
<jsp:directive.page import="java.util.Set"/>
<jsp:directive.page import="java.util.HashSet"/>
<jsp:directive.page import="java.util.Arrays"/>
<jsp:directive.page import="com.winterwar.base.Event"/>
<jsp:directive.page import="com.winterwar.base.Slot"/>
<jsp:directive.page import="com.winterwar.base.Convention"/>
<jsp:directive.page import="com.winterwar.base.User"/>
<jsp:directive.page import="java.text.NumberFormat"/>

<%if(session.getAttribute("randFormID") == null){
	Random gen = new Random(new Date().getTime());
	session.setAttribute("randFormID", gen.nextInt() + "");
}
Set<Slot> slots = ((Convention) request.getAttribute("currentCon")).getSlots();
Set<Event> events = ((Convention) request.getAttribute("currentCon")).getEvents();

Double singleDay = ((Convention) request.getAttribute("currentCon")).getSingleDayCost();
Double weekend = ((Convention) request.getAttribute("currentCon")).getWeekendPassCost();
Double aslWeekend = ((Convention) request.getAttribute("currentCon")).getWeekendASLCost();

NumberFormat nf = NumberFormat.getCurrencyInstance();
Set<Event> userEvents = new HashSet<Event>();
User user = (User) request.getAttribute("user");
if(user != null){
	userEvents = user.getSubmittedEvents();
}
%>


<title>Winter War - Registration</title>

<h1>Winter War Convention Registration</h1>
<%if(session.getAttribute("userID") == null) { %>
<hr/>
<p> We're sorry, but this feature is only available to users who are logged in.  Please either log in below, or create a new account! We'd love to have you!
</p>
<html:form action="/user" focus="user.username">
	<input type="hidden" name="method" value="login"/>
	<table>
		<tr>
			<th><bean:message key="user.username"/>: </th>
			<td><html:text property="user.username"/></td>
			<th><bean:message key="user.password"/>: </th>
			<td><html:password property="user.password"/></td>
		</tr>
		<tr>
			<td><html:submit styleClass="button">Login</html:submit></td>
			<td><input type="button" value="New Account" onclick="document.location.href='./user.do?method=edit'"/></td>
		</tr>
	</table>
</html:form>
<%} else if(request.getAttribute("regExist") != null) { %>
<h2>Oops! Seems you've been here before.</h2>
<p>We're sorry, but it appears that you have already registered for the current convention.
</p><p>  If this information is in error, please contact the Convention Registrar's office to confirm that there is an error.</p>
<%} else { %>
<p>While the Winter War convention staff has gone to great lengths to insure the accuracy of the information in this pre-registration schedule, events may be cancelled or changed. The fastest way to get the most up-to-date information about Winter War is by accessing the Winter War convention website. Changes to the schedule will be posted to our website within 48 hours when possible.</p>
<html:form action="/registration">
	<input type="hidden" name="method" value="confirm"/>
	<input type="hidden" name="formID" value="<%=session.getAttribute("randFormID") %>"/>
	<input type="hidden" name="registration.conID" value="${conID }"/>
	<input type="hidden" name="registration.userID" value="${userID }"/>
	<input type="hidden" name="registration.cost" value="0.0"/>
	<input type="hidden" name="registration.costOffset" value="0.0"/>
	<html:hidden property="registration.status" value="1"/>
	<html:hidden property="registration.regID"/>
	<table>
		<tr>
			<th>Registrant Name</th> <td colspan="3">${user.firstName } ${user.lastName }</td>
		</tr><tr>
			<th>Address</th> <td colspan="3">${user.address1 }</td>
		</tr><tr>
			<th>City/State/Postal Code</th> <td colspan="3">${user.city }, ${user.state } ${user.zipCode }</td>
		</tr><tr>
			<th>Contact Info</th> <td colspan="3">${user.email } ${user.phoneNumber }</td>
		</tr><tr>
			<td colspan="4"><hr/></td>
		</tr><tr>
			<th><bean:message key="registration.type"/>: </th>
			<td><html:radio property="registration.type" value="1" onclick="swapDays(false)">Single Day (<%=nf.format(singleDay) %>)</html:radio><br/>
				<html:radio property="registration.type" value="2" onclick="swapDays(true)">Weekend (<%=nf.format(weekend) %>)</html:radio><br/>
				<html:radio property="registration.type" value="3" onclick="swapDays(true)">ASL Weekend (<%=nf.format(aslWeekend) %>)</html:radio></td>
			<th><bean:message key="registration.dayRegistered"/>: </th>
			<td>
				<html:radio property="registration.dayRegistered" value="1" disabled="true">Friday</html:radio><br/>
				<html:radio property="registration.dayRegistered" value="2" disabled="true">Saturday</html:radio><br/>
				<html:radio property="registration.dayRegistered" value="3" disabled="true">Sunday</html:radio><br/>
			</td>
		</tr>
		<%int slotCount = 0;
		boolean judging;
		for(Slot slot: slots){ 
			judging = false;
			slotCount++;
			for(Event e : userEvents){
				if(e.getStatus().intValue() == 3 && e.getSlotID().intValue() == slot.getSlotID().intValue()){
					judging = true;
				}
			}%>
		<tr>
			<th><%=slot.getSlotName() %></td>
			<td colspan="3">
				<% if(judging){ %>- You are currently running an event in this slot.
				<%} else { %>
				<select name="events" id="<%=slotCount %>" onChange="determineSlot()" style="width:75%">
					<option value="0">None</option>
					<%for(Object o: slot.getEvents()){ 
						Event event = (Event) o;
						if(event.getStatus() == 3 && event.getReservedSeats() < event.getTotalSeats()){ %>
							<option value="<%=event.getEventID() %>"><%=event.getEventName() %><%if(event.getSlots() > 1){ %> (<%=event.getSlots() %> Slots)<%} %></option>
					<%	} 
					}%>
				</select>&nbsp;<a href="javascript:popupSlotView(<%=slotCount %>, <%=slot.getSlotID() %>)" style="font-size:10pt">(Full Details...)</a>
			<%} %>
			</td>
		</tr>
		<%} %>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td><html:submit>Submit Registration</html:submit></td>
		</tr>
	</table>
</html:form>
<%} %>
<script type="text/javascript" language="javascript">

	function determineSlot(){
		var fields = document.getElementsByName("events");
		var disableCount = 0;
		for(i=0;i<fields.length;i++){
			fields[i].disabled = false;
		}

		for(i=0;i<fields.length;i++){
			if(disableCount > 0){
				fields[i].value = 0;
				fields[i].disabled = true;
				disableCount--;
			} else {
				disableCount = getDisableCount(fields[i].value);
				if(disableCount < 0) disableCount = 0;
			}
		}		
	}

	function getDisableCount(id){
		switch(id-0){
			<%for(Event e : events){%>
			case <%=e.getEventID()%>: 
				return <%=e.getSlots() -1%>;
			<%}%>
			default: return 0;
		}
	}

	function swapDays(onoff){
		var fields = document.getElementsByName("registration.dayRegistered");
		for(i=0;i<fields.length;i++)
			fields[i].disabled = onoff;
	}

	function updateReg(slotID, eventID){
		document.getElementById(slotID).value=eventID;
		determineSlot();
	}

	function popupSlotView(parentSlotID, slotID){
		window.open("popup/slot.do?method=slotView&parentSlotID="+parentSlotID+"&slotID="+slotID,"EventPicker",
		'height=450,width=820,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,modal=yes', true);
	}
</script>
